package com.community.backend.controller;

import com.community.backend.pojo.Notice;
import com.community.backend.pojo.dto.NoticeQueryDTO;
import com.community.backend.service.NoticeService;
import com.community.backend.utils.PageResult;
import com.community.backend.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
公告
 */
@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @GetMapping("/page")
    public Result<PageResult> page(NoticeQueryDTO noticeQueryDTO) {
        log.info("公告分页信息：{}", noticeQueryDTO);
        //DOTO:在有效期结束后应该不展示 这个应该是用户界面不展示
        //另外 我们的状态 有已过期，这个怎么弄
        PageResult pageResult = noticeService.pageQuery(noticeQueryDTO);
        return Result.success(pageResult);
    }
    @GetMapping("/list")
    public Result<List<Notice>> list() {

        //DOTO:在有效期结束后应该不展示 这个应该是用户界面不展示
        //另外 我们的状态 有已过期，这个怎么弄
        List<Notice> list = noticeService.list();
        return Result.success(list);
    }
    @PostMapping
    public Result save(@RequestBody Notice notice) {
        log.info("添加公告信息：{}", notice);
        noticeService.save(notice);
        return Result.success("添加成功");
    }
    @PutMapping
    public Result NoticeUpdate(@RequestBody Notice notice) {
        log.info("修改公告信息：{}", notice);
        noticeService.update(notice);
        return Result.success();
    }
    @PutMapping("/sort")
    public Result<Void> top(@RequestParam Integer noticeId,
                            @RequestParam Integer sort) {
        noticeService.updateSort(noticeId, sort);
        return Result.success();
    }
    //重置密码
//    @PutMapping("/resetPwd/{id}")
//    public Result resetPwd(@PathVariable Integer id) {
//        Security security = new Security();
//        security.setId(id);
//        security.setPassword("123456");
//        return securityService.updateById(security);
//    }
    @DeleteMapping("/{noticeId}")
    public Result NoticeDeleteById(@PathVariable Integer noticeId) {
        log.info("删除公告信息：{}", noticeId);
        noticeService.deleteById(noticeId);
        return Result.success("删除成功");
    }
    @GetMapping("/{noticeId}")
    public Result<Notice> NoticeDetail(@PathVariable Integer noticeId) {
        Notice notice = noticeService.NoticeDetail(noticeId);
        return Result.success(notice);
    }
}
